<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/admin/attribute-template/list">属性模板管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>添加属性模板</el-breadcrumb-item>
    </el-breadcrumb>

    <h3 style="margin: 20px 0;">添加属性模板</h3>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="150px" class="demo-ruleForm">
      <el-form-item label="属性模板名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="属性模板名称的拼音" prop="pinyin">
        <el-input v-model="ruleForm.pinyin"></el-input>
      </el-form-item>
      <el-form-item label="关键词列表" prop="keywords">
        <el-input v-model="ruleForm.keywords"></el-input>
      </el-form-item>
      <el-form-item label="自定义排序序号" prop="sort">
        <el-input v-model="ruleForm.sort"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        ruleForm: {
          name: '',
          pinyin: '',
          keywords: '',
          sort: ''
        },
        rules: {
          name: [
            {required: true, message: '请输入属性模板名称', trigger: 'blur'},
            {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let url = 'http://localhost:9080/attribute-templates/add-new';
            console.log('url >>> ' + url);
            console.log('data >>> ');
            console.log(this.ruleForm);
            this.axios
                    .create({headers: {'Authorization': localStorage.getItem('jwt')}})
                    .post(url, this.ruleForm).then((response) => {
              console.log(response.data);
              if (response.data.code == 20000) {
                this.$message({
                  message: '添加属性模板成功！',
                  type: 'success'
                });
                this.resetForm(formName);
              } else {
                this.$message.error(response.data.message);
              }
            }).catch(function (error) {
              console.log('响应结果为失败！');
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style scoped>

</style>